package com.example.teachingSystem.controller;

import com.example.teachingSystem.pojo.form.course.*;
import com.example.teachingSystem.pojo.vo.OptionVo;
import com.example.teachingSystem.pojo.vo.course.CourseVo;
import com.example.teachingSystem.service.CourseService;
import com.example.teachingSystem.service.EnrollmentService;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@Tag(name = "课程模块")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private EnrollmentService enrollmentService;

    @PostMapping("one")
    @Operation(summary = "添加课程", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> addCourse(@RequestBody @Validated AddCourseForm addCourseForm) {
        return courseService.addCourse(addCourseForm);
    }

    @PutMapping("one")
    @Operation(summary = "修改课程", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> updateCourse(@RequestBody @Validated UpdateCourseForm updateCourseForm) {
        return courseService.updateCourse(updateCourseForm);
    }

    @PutMapping("one/{id}")
    @Operation(summary = "根据id删除课程", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> deleteCourse(@PathVariable Long id) {
        return courseService.deleteCourse(id);
    }

    @GetMapping("/more/stu")
    @Operation(summary = "学生获取课程", security = {@SecurityRequirement(name = "Authorization")})
    public PageResult<CourseVo> getMyCoursePageByStu(CoursePageQuery coursePageQuery) {
        return courseService.getMyCoursePageByStu(coursePageQuery);
    }

    @GetMapping("/more/tea")
    @Operation(summary = "老师获取课程", security = {@SecurityRequirement(name = "Authorization")})
    public PageResult<CourseVo> getMyCoursePageByTea(CoursePageQuery coursePageQuery) {
        return courseService.getMyCoursePageByTea(coursePageQuery);
    }

    @GetMapping("/more")
    @Operation(summary = "管理员获取课程", security = {@SecurityRequirement(name = "Authorization")})
    public PageResult<CourseVo> getAllCoursePage(CoursePageQuery coursePageQuery) {
        return courseService.getAllCoursePage(coursePageQuery);
    }

    @PostMapping("/select/stu")
    @Operation(summary = "学生选课", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> addEnrollmentByStu(@RequestBody @Validated AddEnrollmentForm addEnrollmentForm) {
        return enrollmentService.addEnrollment(addEnrollmentForm,false);
    }

    @PutMapping("/select/stu")
    @Operation(summary = "学生退课", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> deleteEnrollmentByStu(@RequestBody DeleteEnrollmentForm deleteEnrollmentForm) {
        return enrollmentService.deleteEnrollment(deleteEnrollmentForm.getEnrollmentIds(),false);
    }

    @PostMapping("/select")
    @Operation(summary = "选课", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> addEnrollment(@RequestBody @Validated AddEnrollmentForm addEnrollmentForm) {
        return enrollmentService.addEnrollment(addEnrollmentForm,true);
    }

    @PutMapping("/select")
    @Operation(summary = "退课", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> deleteEnrollment(@RequestBody DeleteEnrollmentForm deleteEnrollmentForm) {
        return enrollmentService.deleteEnrollment(deleteEnrollmentForm.getEnrollmentIds(),true);
    }

    @GetMapping("/option/{semesterId}")
    @Operation(summary = "学生获取可选课程", security = {@SecurityRequirement(name = "Authorization")})
    public Result<List<CourseVo>> getOptionalCoursesByStu(@PathVariable Long semesterId) {
        return courseService.getOptionalCoursesByStu(semesterId);
    }

    @GetMapping("/tea/option")
    @Operation(summary = "教师选项", security = {@SecurityRequirement(name = "Authorization")})
    public Result<List<OptionVo>> getTeacherOptions() {
        return courseService.getTeacherOptions();
    }
}
